/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iandtic.paydnie.ui;

import iandtic.paydnie.adapters.Adapter;
import iandtic.paydnie.dnie.CardListener;
import iandtic.paydnie.dnie.DNIe;
import iandtic.paydnie.dnie.DNIeConstructor;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.smartcardio.CardException;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import sun.awt.WindowClosingListener;
import sun.awt.windows.ThemeReader;


    
    
    
    /**
 *
 * @author sergio
 */
public class POSInterface extends javax.swing.JFrame {

    
    CardListener cardl=null;
    Adapter adp=null;
    int OK= 1;



/**
     * Creates new form POSInterface
     */
    public POSInterface() throws CardException {
        initComponents();
        
        cardl= new CardListener(this); 
        Thread t= new Thread(cardl);
        t.start();
        
    }
    
    public void dnieInserted() {
        this.requestPIN();
    }
    
    public void requestPIN(){
        TextInfo.setText("Introduzca el PIN");        
    }
          
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        keyboard = new javax.swing.JPanel();
        key1 = new javax.swing.JButton();
        key2 = new javax.swing.JButton();
        key3 = new javax.swing.JButton();
        key4 = new javax.swing.JButton();
        key5 = new javax.swing.JButton();
        key6 = new javax.swing.JButton();
        key7 = new javax.swing.JButton();
        key8 = new javax.swing.JButton();
        key9 = new javax.swing.JButton();
        keyOk = new javax.swing.JButton();
        keyCancel = new javax.swing.JButton();
        screen = new javax.swing.JPanel();
        screenLayeredPane = new javax.swing.JLayeredPane();
        ScrollPane = new javax.swing.JScrollPane();
        CardsList = new javax.swing.JList();
        TextInfo = new javax.swing.JTextField();
        pinField = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        key1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key1.setText("1");
        key1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key1ActionPerformed(evt);
            }
        });

        key2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key2.setText("2");
        key2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key2ActionPerformed(evt);
            }
        });

        key3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key3.setText("3");
        key3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key3ActionPerformed(evt);
            }
        });

        key4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key4.setText("4");
        key4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key4ActionPerformed(evt);
            }
        });

        key5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key5.setText("5");
        key5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key5ActionPerformed(evt);
            }
        });

        key6.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key6.setText("6");
        key6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key6ActionPerformed(evt);
            }
        });

        key7.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key7.setText("7");
        key7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key7ActionPerformed(evt);
            }
        });

        key8.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key8.setText("8");
        key8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key8ActionPerformed(evt);
            }
        });

        key9.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        key9.setText("9");
        key9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                key9ActionPerformed(evt);
            }
        });

        keyOk.setBackground(new java.awt.Color(0, 204, 51));
        keyOk.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        keyOk.setText("OK");
        keyOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keyOkActionPerformed(evt);
            }
        });

        keyCancel.setBackground(new java.awt.Color(255, 51, 51));
        keyCancel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        keyCancel.setText("X");
        keyCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keyCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout keyboardLayout = new javax.swing.GroupLayout(keyboard);
        keyboard.setLayout(keyboardLayout);
        keyboardLayout.setHorizontalGroup(
            keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(keyboardLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(keyboardLayout.createSequentialGroup()
                        .addComponent(key1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(key2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(key3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(keyCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(keyboardLayout.createSequentialGroup()
                        .addGroup(keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(keyboardLayout.createSequentialGroup()
                                .addComponent(key7, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(key8, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(key9, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(keyboardLayout.createSequentialGroup()
                                .addComponent(key4, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(key5, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(key6, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(keyOk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(18, 18, 18))
        );
        keyboardLayout.setVerticalGroup(
            keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(keyboardLayout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(key1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(key3, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(key2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(keyCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(keyboardLayout.createSequentialGroup()
                        .addGroup(keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(key4, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(key5, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(key6, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(keyboardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(key7, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(key8, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(key9, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(keyOk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        screen.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        CardsList.setFont(new java.awt.Font("Consolas", 0, 14)); // NOI18N
        CardsList.setToolTipText("");
        CardsList.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        ScrollPane.setViewportView(CardsList);

        ScrollPane.setBounds(0, 0, 250, 230);
        screenLayeredPane.add(ScrollPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TextInfo.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N
        TextInfo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        TextInfo.setToolTipText("");
        TextInfo.setBorder(null);
        TextInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextInfoActionPerformed(evt);
            }
        });
        TextInfo.setBounds(10, 10, 230, 180);
        screenLayeredPane.add(TextInfo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        pinField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pinField.setBorder(null);
        pinField.setBounds(31, 160, 180, 60);
        screenLayeredPane.add(pinField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout screenLayout = new javax.swing.GroupLayout(screen);
        screen.setLayout(screenLayout);
        screenLayout.setHorizontalGroup(
            screenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(screenLayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
        );
        screenLayout.setVerticalGroup(
            screenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(screenLayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
        );

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iandtic/paydnie/ui/paydnie_logo.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(keyboard, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(screen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(screen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(keyboard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void key1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key1ActionPerformed
       pinField.setText(pinField.getText()+"1");
    }//GEN-LAST:event_key1ActionPerformed

    private void key2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key2ActionPerformed
       pinField.setText(pinField.getText()+"2");
    }//GEN-LAST:event_key2ActionPerformed

    private void key3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key3ActionPerformed
       pinField.setText(pinField.getText()+"3");
    }//GEN-LAST:event_key3ActionPerformed

    private void key4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key4ActionPerformed
       pinField.setText(pinField.getText()+"4");
    }//GEN-LAST:event_key4ActionPerformed

    private void key5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key5ActionPerformed
       pinField.setText(pinField.getText()+"5");
    }//GEN-LAST:event_key5ActionPerformed

    private void key6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key6ActionPerformed
       pinField.setText(pinField.getText()+"6");
    }//GEN-LAST:event_key6ActionPerformed

    private void key7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key7ActionPerformed
       pinField.setText(pinField.getText()+"7");
    }//GEN-LAST:event_key7ActionPerformed

    private void key8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key8ActionPerformed
       pinField.setText(pinField.getText()+"8");
    }//GEN-LAST:event_key8ActionPerformed

    private void key9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_key9ActionPerformed
       pinField.setText(pinField.getText()+"9");
    }//GEN-LAST:event_key9ActionPerformed

    private void keyCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyCancelActionPerformed
       pinField.setText("");
    }//GEN-LAST:event_keyCancelActionPerformed

    private void TextInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextInfoActionPerformed
        //TODO
    }//GEN-LAST:event_TextInfoActionPerformed

    private void keyOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyOkActionPerformed
        
        if(OK==1){
            //TextInfo.setText("Comprobando...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(POSInterface.class.getName()).log(Level.SEVERE, null, ex);
            }

            //creacion del objeto dni y envio del objeto
            //DNIe dnie= new DNIe();
            //DNIeConstructor dnieCons= new DNIeConstructor();
            //dnieCons.setDNIeNIFandPIN(dnie,Integer.parseInt((pinField.getText())));

            System.out.println("PIN marcado es: "+Integer.parseInt((pinField.getText())));
            pinField.setText("");
            TextInfo.setText("");
            TextInfo.hide();
            //adp.sendConfirmationRequest(Integer.parseInt((pinField.getText())));

            String[] arr = {"Visa Money/LaCaixa - 4030513..", "Tarjeta Azul/Sabadell - 8918..", "Carnet Jove/LaCaixa - 019181.."};
            CardsList.setListData(arr); 
            OK=2;
        
        } else {
            String[] arr = {""};
            CardsList.setListData(arr); 
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(POSInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
            TextInfo.setText("Pago realizado");
            TextInfo.show();
        }
    }//GEN-LAST:event_keyOkActionPerformed

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(POSInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(POSInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(POSInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(POSInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new POSInterface().setVisible(true);
                } catch (CardException ex) {
                    Logger.getLogger(POSInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList CardsList;
    private javax.swing.JScrollPane ScrollPane;
    private javax.swing.JTextField TextInfo;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton key1;
    private javax.swing.JButton key2;
    private javax.swing.JButton key3;
    private javax.swing.JButton key4;
    private javax.swing.JButton key5;
    private javax.swing.JButton key6;
    private javax.swing.JButton key7;
    private javax.swing.JButton key8;
    private javax.swing.JButton key9;
    private javax.swing.JButton keyCancel;
    private javax.swing.JButton keyOk;
    private javax.swing.JPanel keyboard;
    private javax.swing.JPasswordField pinField;
    private javax.swing.JPanel screen;
    private javax.swing.JLayeredPane screenLayeredPane;
    // End of variables declaration//GEN-END:variables




}
